Todo.Item = Todo.Utility.createClass(
    /* initializer */
    function Store(options) {

        /* "abstract" class, basic windows options and settings here  */
        this.options = {
            itemSelector: '#lists li.item'
        }

        this.options = $.extend(true, this.options, options);

        this.init();
    },
    /* methods */
    {
        init: function () {
            this.bindEvents();
            this.triggerEvents()
        },

        bindEvents: function () {
            var self = this;
            $(document).on({
                    'startEdit.Todo' : self.startEdit,
                    'cancelEdit.Todo': self.cancelEdit,
                    'checked.Todo'   : self.checked,
                    'rename.Todo'    : self.rename,
                    'delete.Todo'    : self.delete
                },
                self.options.itemSelector);
        },

        triggerEvents: function () {
            var self = this;
            /**
             * Start editing item
             */
            $(document).on('dblclick', self.options.itemSelector + ' span.name', function () {
                $(this).parents(self.options.itemSelector).trigger('startEdit.Todo');
            });

            /**
             * Item check
             */
            $(document).on('click', self.options.itemSelector + ' label', function () {
                $(this).parents(self.options.itemSelector).trigger('checked.Todo');
            });

            /**
             * Item edit
             */
            var onReturn = 0;

            $(document).on({
                'keydown': function (e) {
                    $item = $(this).parents(self.options.itemSelector);

                    if (e.keyCode == 13) {
                        onReturn = 1;

                        $item.trigger('rename.Todo')
                        //itemEdit();
                        //categoryEdit();

                        EnableSync = 1;
                    }

                    if (e.keyCode == 27) {
                        $item.trigger('cancelEdit.Todo');
                        EnableSync = 1;
                    }
                },
                'blur'   : function () {
                    $item = $(this).parents(self.options.itemSelector);

                    if (onReturn == 0) {
                        $($item).trigger('rename.Todo');
                        //itemEdit();
                        //categoryEdit();
                    } else {
                        onReturn = 0;
                    }
                }
            }, self.options.itemSelector + ' textarea:visible');


            /**
             * Click delete button
             */
            $(document).on('click', self.options.itemSelector + ' .edit .delete', function () {
                console.log(1);
                $(this).parents(self.options.itemSelector).trigger('delete.Todo');
            });
        },

        startEdit: function (event) {
            if (Todo.DEBUG_MODE) {
                console.log('Todo.Item.startEdit', this);
            }

            $item = $(this);

            $item.addClass('editing');

            $textarea = $item.find('.edit textarea');

            $('#return').html($textarea.val().replace(/(<([^>]+)>)/ig, ""));

            $textarea.focus().select();
        },

        cancelEdit: function (event) {
            if (Todo.DEBUG_MODE) {
                console.log('Todo.Item.cancelEdit', this);
            }

            $(this).removeClass('editing');
        },

        rename: function (event) {
            if (Todo.DEBUG_MODE) {
                console.log('Todo.Item.rename', this);
            }

            $item = $(this);
            var element = $item.find('textarea:visible');
            var itemId = $item.attr('data-internal-id');

            if (itemId) {
                if ($(element).val() == '') {
                    $item.trigger('delete.Todo');
                    //self.storage.updateItem(itemId, 'delete');
                } else if ($(element).val().replace(/(<([^>]+)>)/ig, "") != $('#return').html()) {
                    $item.trigger('rename.Todo');
                    //self.storage.updateItem(itemId, 'edit', $(element).val().replace(/(<([^>]+)>)/ig, ""));
                } else {

                    //$(element).parents('li').removeClass('editing');
                }
            }

            $item.removeClass('editing');
        },

        checked: function (event) {
            if (Todo.DEBUG_MODE) {
                console.log('Todo.Item.checked', this);
            }

            return;
            var itemId = $(this).attr('data-internal-id');

            self.storage.updateItem(itemId, 'complete');
        },

        delete: function (event) {
            if (Todo.DEBUG_MODE) {
                console.log('Todo.Item.delete', this);
            }
        }
    }
);